#include <emscripten.h>
#include <iostream>
#include "EngineWebGPU.h"


const char* shaderSource = R"(
@vertex
fn vs_main(@builtin(vertex_index) vertexIndex: u32) -> @builtin(position) vec4f {
    let pos = array<vec2f, 3>(
        vec2f(0.0, 0.5),
        vec2f(-0.5, -0.5),
        vec2f(0.5, -0.5)
    );
    return vec4f(pos[vertexIndex], 0.0, 1.0);
}

@fragment
fn fs_main() -> @location(0) vec4f {
    return vec4f(0.0, 1.0, 0.0, 1.0);
}
)";
EngineWebGPU engineWebGPU;

wgpu::RenderPipeline renderPipeline = nullptr;

void loop_iteration(void* user_data) {
  if (renderPipeline) {
    engineWebGPU.loop(std::move(renderPipeline));
  }
}

int main() {
  emscripten_set_main_loop_arg(loop_iteration, nullptr, -1, false);
  return 0;
}

extern "C"
{
  EMSCRIPTEN_KEEPALIVE
    void init() {
    engineWebGPU.init();

    wgpu::ShaderModule shaderModule = engineWebGPU.createShaderModule(shaderSource);
    wgpu::PipelineLayout pipelineLayout = engineWebGPU.createPipelineLayout();
    renderPipeline = engineWebGPU.createRenderPipeline(pipelineLayout, std::move(shaderModule));
  }
}
